import { MVC } from "../../framework/MVC";
import { AlertManager, AlertType } from "../../alert/AlertManager";
import { Util } from "../../utils/Util";
import { GameVoManager } from "../../manager/GameVoManager";
import { Manager } from "../../manager/Manager";
import NetAdapter from "../../adpapter/NetAdapter";
import { Cfg } from "../../config/Cfg";
import GlobalData from "../../GlobalData";
import { Time } from "../../framework/Time";
import GuajiManager from "../../manager/GuajiManager";
import AdManager from "../../manager/AdManadgr";
import { Const } from "../../config/Const";
import { UIManager } from "../../framework/UIManager";
import { Common_UIPath } from "../../common/Common_Define";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GuajiView extends MVC.BaseView {

    @property(cc.Button)
    btnGetPower: cc.Button = null;

    @property(cc.Button)
    btnGuaji1: cc.Button = null;

    @property(cc.Label)
    time: cc.Label = null;

    protected changeListener(enable: boolean): void {
    }

    /*
     * 打开界面回调，每次打开只调用一次
     */
    public onOpen(args: any): void {
        super.onOpen(args);

        let date = true ? new Date() : new Date(Time.serverTimeMs);
        let curday = date.getDate();
        if (curday != GameVoManager.getInstance.myUserVo.day) {
            console.log("重置每日领取体力");
            GameVoManager.getInstance.myUserVo.getGuaPopwer = false;
            GameVoManager.getInstance.myUserVo.guajiInter = false;

        }

        if (!GameVoManager.getInstance.myUserVo.guajiInter) {
            AdManager.inst.playInterstitial(() => {
                console.log("插屏播放完毕");
                GameVoManager.getInstance.myUserVo.guajiInter = true;
            });
        }

        this.btnGetPower.interactable = !GameVoManager.getInstance.myUserVo.getGuaPopwer;

        if (GameVoManager.getInstance.myUserVo.guajiTime > 0) {
            this.btnGuaji1.interactable = false;
        } else {
            this.btnGuaji1.interactable = true;
        }
    }

    onGetPower() {
        GameVoManager.getInstance.myUserVo.getGuaPopwer = true;
        this.btnGetPower.interactable = !GameVoManager.getInstance.myUserVo.getGuaPopwer;
        GameVoManager.getInstance.setPower(24, 2);
        AlertManager.showNormalTips("领取成功");
    }

    onShowSelectAlert() {
        if (GameVoManager.getInstance.myUserVo.power < 12) {
            AlertManager.showNormalTips("体力不足");
            return
        }
        this.onClose();
        UIManager.Open(Common_UIPath.GuajiSelected);
    }

    onbtnStop() {
        //随机给点金币和钻石 默认
        // GameVoManager.getInstance.myUserVo.guaji5bei = true;
        // GuajiManager.inst.getDrop(5);
        // return;
        //挂机消耗
        //获取挂机奖励
        let cost = Const.GuajiPowerCost;
        if (GameVoManager.getInstance.myUserVo.guaji5bei) {
            cost *= 5;
        }

        if (GameVoManager.getInstance.myUserVo.guajiTime > 0) {
            //挂机小时有几个
            let count = this.hoursSinceTimestamp(GameVoManager.getInstance.myUserVo.guajiTime);
            if (count <= 0) {
                AlertManager.showNormalTips("挂机不足一小时");
                return;
            }
            //体力够挂机几小时
            let total = Math.floor(GameVoManager.getInstance.myUserVo.power / cost);
            if (total <= 0) {
                AlertManager.showNormalTips("体力不够领取挂机奖励");
                return;
            }
            //二者去最小值
            let realCount = Math.min(count, total);
            if (realCount >= 1) {
                GameVoManager.getInstance.setPower(realCount * cost, 3);
                GuajiManager.inst.getDrop(realCount);
                this.btnGuaji1.interactable = true;
                GameVoManager.getInstance.myUserVo.guajiTime = 0;
            } else {
                if (count <= 0) {
                    AlertManager.showNormalTips("挂机不足一小时");
                } else {
                    AlertManager.showNormalTips("体力不够领取挂机奖励");
                }
            }
        } else {
            AlertManager.showNormalTips("你还没有开始挂机");
        }
    }

    guaji() {
        this.btnGuaji1.interactable = false;
        GameVoManager.getInstance.myUserVo.guajiTime = Date.now(); // 获取当前时间戳
    }

    hoursSinceTimestamp(targetTimestamp) {
        const currentTimestamp = Date.now(); // 获取当前时间戳
        const diffMilliseconds = currentTimestamp - targetTimestamp;
        const diffHours = Math.floor(diffMilliseconds / (1000 * 60 * 60));
        return Math.max(0, diffHours);
    }

    getTimePassed(timestamp) {
        timestamp /= 1000

        // 获取当前时间戳
        const now = Math.floor(Date.now() / 1000); // 转换为秒

        // 计算时间差（单位：秒）
        const difference = now - timestamp;

        // 转换时间差为适当的单位
        const seconds = Math.floor(difference % 60);
        const minutes = Math.floor(difference / 60) % 60;
        const hours = Math.floor(difference / 3600) % 24;
        const days = Math.floor(difference / 86400);

        // 格式化输出
        let result = '';
        if (days > 0) {
            result += `${days} 天 `;
        }
        if (hours > 0) {
            result += `${hours} 小时 `;
        }
        if (minutes > 0) {
            result += `${minutes} 分钟 `;
        }
        if (seconds > 0) {
            result += `${seconds} 秒 `;
        }

        return result.trim();
    }


    /*
     * 关闭界面回调，每次打开只调用一次
     */
    public onClose(): void {
        super.onClose();
        Manager.audio.playAudio(501);
    }

    protected update(dt: number): void {
        if (GameVoManager.getInstance.myUserVo.guajiTime > 0) {
            let str = this.getTimePassed(GameVoManager.getInstance.myUserVo.guajiTime);
            this.time.string = str;
        } else {
            this.time.string = "0";
        }
    }
}
